package org.geektimes.projects.user.service.impl;

import org.geekbang.projects.orm.MapperBuild;
import org.geektimes.base.id.IdGenerator;
import org.geektimes.base.proxy.AopProxyContext;
import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.repository.UserMapper;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.support.Component;
import org.geektimes.projects.user.support.transaction.Transaction;
import org.geektimes.projects.user.web.controller.UserController;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.QueryStructure;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.validation.Valid;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* 用户管理  
* @author : KangNing Hu
*/
@Component
public class UserServiceImpl implements UserService {


	private static Logger logger = Logger.getLogger(UserServiceImpl.class.getName());



	/// TODO 循环依赖目前只做了校验，未做延迟注入实现
	@Resource(name = "jpa/entityManager")
	private EntityManager entityManager;



	@PostConstruct
	public void init(){
		logger.log(Level.INFO , "初始化" + entityManager);
		System.out.println("初始化");
	}

	/**
	 * 获取所有用户
	 * @return
	 */
	@Override
	public List<User> queryAll() {
		return entityManager
				.createNativeQuery("select  * from users" , User.class).unwrap(NativeQueryImpl.class)
				.getResultList();
	}


	/**
	 * 注册
	 * @param user 用户对象
	 * @return
	 */
	@Override
	@Transaction
	public boolean register(@Valid User user) {
		EntityTransaction transaction = entityManager.getTransaction();
			entityManager.persist(user);
			return true ;

	}

	@Override
	public boolean deregister(User user) {
		return false;
	}

	@Override
	public boolean update(User user) {
		return false;
	}



	@Override
	public User queryUserById(Long id) {
		return null;
	}

	@Override
	public User queryUserByNameAndPassword(String name, String password) {
		return null;
	}
}
